const {CompanyModel} = require('../../model/CompanySchema/Company');

// 添加公司信息
exports.Add = function (arg) {
    return new Promise((resolve, reject) => {
        const companyModel = new CompanyModel({
            CompanyLogo: arg.CompanyLogo,
            CompanyName: arg.CompanyName,
            CompanyIntroduce: arg.CompanyIntroduce,
            CompanyInImgs: arg.CompanyInImgs,
            CompanyRegion: arg.CompanyRegion,
            CompanyRegionNumber: arg.CompanyRegionNumber,
            CompanyAdress: arg.CompanyAdress,
            CompanyPhone: arg.CompanyPhone,
            CompanyRegisterTime: arg.CompanyRegisterTime,
            CompanyRegisterMoney: arg.CompanyRegisterMoney,
            CompanyLegalPerson: arg.CompanyLegalPerson,
            CompanyScale: arg.CompanyScale,
            CompanyFree: arg.CompanyFree,
            SocialUnifiedCreditCode: arg.SocialUnifiedCreditCode,
            CompanyIndustry: arg.CompanyIndustry
        })
        companyModel.save((error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
};

/* 查询用户信息(模糊/分页/多条件)
* */
exports.Sel = function (PageNumber,PageSize,arg) {
    return new Promise((resolve, reject) => {
        CompanyModel.find({...arg}).skip(PageNumber * PageSize).limit(PageSize).exec((error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
};

// 查询文档中的对象/数组的属性
/*
*   @key           需要匹配的键名
*   @value         需要匹配的值
*   @filterOptions 需要过滤的属性
* */
exports.Sel_quoteType = function (key, value, filterOptions) {
    return new Promise((resolve, reject) => {
        CompanyModel.find({[key]: value}, filterOptions, (error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
}

// 查询count
exports.Sel_Count = function (arg) {
    return new Promise((resolve, reject) => {
        CompanyModel.count({...arg}, (error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
}

// 修改公司信息
exports.Upd = function (_id, UpdateObject) {
    return new Promise((resolve, reject) => {
        CompanyModel.findByIdAndUpdate({_id}, UpdateObject, (error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
}

// 删除公司信息(删除单条)
exports.Del_One_ById = function (value) {
    return new Promise((resolve, reject) => {
        CompanyModel.deleteOne({'_id':value}, (error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
}
// 删除公司信息(删除多条)
exports.Del_Many_ById = function (valueList) {
    return new Promise((resolve, reject) => {
        CompanyModel.deleteMany({'_id':{$in:valueList}}, (error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
}